package cn.jianyongjie;

import cn.jianyongjie.util.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

/**
* @Description: TODO
* @Author: JianYongJie
* @Version: 1.0
* @Date:  2022/5/27 17:01
*/
public class EsTest_02 {


    /*
    * @Title: EsTest_02
    * @Description: 根据id查询
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/27 17:07
    * @Parameters: * @param
    * @Return void
    */
    @Test
    public void inquiry() throws Exception{
        // 获取客户端对象
        TransportClient client = ESClientUtil.getClient();
        // 连接索引
        GetRequestBuilder getRequestBuilder = client.prepareGet("pet", "pet", "90");
        // 查询数据
        GetResponse fields = getRequestBuilder.get();
        // 打印数据
        System.out.println(fields.getSource());
        // 关闭连接
        client.close();
    }


    /*
    * @Title: EsTest_02
    * @Description: 新增
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/27 17:53
    * @Parameters: * @param
    * @Return void
    */
    @Test
    public void add() throws Exception{

        // 得到客户端对象
        TransportClient client = ESClientUtil.getClient();
        for (int i = 0;i<100;i++){
            // 设置索引，类型，id
            IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pet", "pet", "90"+i);
            // 设置数据
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("id","90"+i);
            map.put("name",i%2==0?"张飞":"关云长");
            map.put("age",45+i);
            map.put("sex",i%3==0?1:0);

            // 进行新增
            IndexResponse indexResponse = indexRequestBuilder.setSource(map).get();
            System.out.println(indexResponse);
        }

        // 关闭连接
        client.close();
    }
    

    /*
    * @Title: EsTest_02
    * @Description: 修改
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/27 18:39
    * @Parameters: * @param
    * @Return void
    */
    @Test
    public void updete() throws Exception{

        // 得到客户端对象
        TransportClient client = ESClientUtil.getClient();
        // 得到索引，类型，id
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pet", "pet", "90");
        // 修改数据
        // 设置数据
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("id","90");
        map.put("name","关云长");
        map.put("age",45);
        map.put("sex",1);

        UpdateResponse updateResponse = updateRequestBuilder.setDoc(map).get();
        System.out.println(updateResponse);
        // 关闭连接
        client.close();
    }


    /*
    * @Title: EsTest_02
    * @Description: 根据id删除
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/27 18:38
    * @Parameters: * @param
    * @Return void
    */
    @Test
    public void test() throws Exception{
        // 获取客户端对象
        TransportClient client = ESClientUtil.getClient();
        // 得到索引
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pet", "pet", "90");
        // 进行删除
        DeleteResponse deleteResponse = deleteRequestBuilder.get();
        System.out.println(deleteResponse);
        // 关闭连接
        client.close();
    }


    /*
    * @Title: EsTest_02
    * @Description: 高级查询
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/27 18:44
    * @Parameters: * @param
    * @Return void
    */
    @Test
    public void advancedSearch() throws Exception{
        // 获取客户端对象
        TransportClient client = ESClientUtil.getClient();
        // 设置索引
        SearchRequestBuilder pet = client.prepareSearch("pet");
        // 设置类型
        pet.setTypes("pet");
        // 当前页数
        pet.setFrom(0);
        // 每页展示条数
        pet.setSize(5);
        // 排序方式为倒序
        pet.addSort("age", SortOrder.DESC);
        // 设置关键字
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        List<QueryBuilder> must = boolQueryBuilder.must();
        must.add(QueryBuilders.matchQuery("name","关云长"));

        // 查询age从140到10000
        List<QueryBuilder> filter = boolQueryBuilder.filter();
        filter.add(QueryBuilders.rangeQuery("age").gte(140).lte(10000));

        // 将查询的参数设置进去
        pet.setQuery(boolQueryBuilder);
        // 进行查询
        SearchResponse searchResponse = pet.get();
        SearchHits hits = searchResponse.getHits();

        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

        // 关闭连接
        client.close();
    }
}
